package com.cat.yygh.hosp.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cat.yygh.common.exception.YyghException;
import com.cat.yygh.common.result.R;
import com.cat.yygh.hosp.service.HospitalSetService;
import com.cat.yygh.common.util.MD5;
import com.cat.yygh.model.hosp.HospitalSet;
import com.cat.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-11-03
 */
@Api(tags = "医院设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Slf4j
public class HospitalSetController {
    //注入服务类
    @Autowired
    private HospitalSetService hospitalSetService;

    /*使用REST风格*/
    @GetMapping("/findAll")
    public R findAll() {
            List<HospitalSet> list = hospitalSetService.list();
            return R.ok().data("key", list);
    }

    @ApiOperation(value = "删除医院")
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@ApiParam(name = "医院id", value = "1", required = true) @PathVariable Integer id) {
        hospitalSetService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "分页条件医院设置列表")
    @PostMapping("/page/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "hospitalSetQueryVo", value = "查询对象", required = false)
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        /*新建一个分页对象*/
        Page<HospitalSet> pageParam = new Page<>(page, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<HospitalSet>();

        /*设置查询条件*/
        if (hospitalSetQueryVo == null) {
            hospitalSetService.page(pageParam, queryWrapper);
        } else {
            /*添加查询条件*/
            String hosname = hospitalSetQueryVo.getHosname();
            String hoscode = hospitalSetQueryVo.getHoscode();

            if (!StringUtils.isEmpty(hosname)) {
                queryWrapper.like("hosname", hosname);
            }

            if (!StringUtils.isEmpty(hoscode)) {
                queryWrapper.eq("hoscode", hoscode);
            }
            hospitalSetService.page(pageParam, queryWrapper);
        }
        /*总共多少条数据*/
        List<HospitalSet> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }
    @ApiOperation(value = "新增医院设置")
    @PostMapping("/save")
    public R save(
            @ApiParam(name = "hospitalSet",value = "医院设置对象",required = true)
            @RequestBody HospitalSet hospitalSet){
        /*设置状态 0不可使用 1可以使用*/
        hospitalSet.setStatus(1);
        /*签名密钥*/
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }
    @ApiOperation(value = "根据ID查医院设置")
    @GetMapping("/getHospSet/{id}")
    public R getById(@ApiParam(name = "id",value = "医院设置id",required = true)
                         @PathVariable String id){
        HospitalSet list = hospitalSetService.getById(id);
        return R.ok().data("item",list);
    }
    @ApiOperation(value = "根据Id修改医院设置")
    @PostMapping("/updateHospitalSet")
    public R updateById(@ApiParam(name = "hospitalSet",value = "医院设置对象",required = true)
                            @RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchRemove")
    public R batchRemove(@RequestBody List<Long> idList){
        boolean b = hospitalSetService.removeByIds(idList);
        return R.ok();
    }
    @ApiOperation(value = "解锁和锁定")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                            @PathVariable Integer status){
        //根据医院id查询信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法修改
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
}

